package com.jie;

import java.util.*;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: 杰
 * @Project :net-disk
 * @DateTime: 2024/5/29 20:00
 **/
public class Main {
    // 共享变量，表示当前应该打印哪个字母
    private static int state = 0;

    // 共享对象，作为锁和通信的媒介
//    private static final Object lock = new Object();

    // 可重入锁
    private static final ReentrantLock lock = new ReentrantLock();

    // 三个条件对象，分别绑定A、B、C三个线程
    private static final Condition A = lock.newCondition();
    private static final Condition B = lock.newCondition();
    private static final Condition C = lock.newCondition();
    public static void main(String[] args) {
        int[][] arr=new int[1][1];
        Arrays.sort(arr,(a,b)->a[0]-b[1]);
    }
    public String decodeString(String s) {
        int multi = 0;
        String current="";
        //记录字符
        LinkedList<String> stk1 = new LinkedList<>();
        //记录每一个数字
        LinkedList<Integer> stk2 = new LinkedList<>();
        char[] charArray = s.toCharArray();
        for (char c : charArray) {
            if (Character.isDigit(c)){
                //记录当前倍数
                multi = multi * 10 + c - '0';
            }else if(c == '['){
                stk1.addFirst(current);
                stk2.addFirst(multi);
                current="";
                multi=0;
            } else if (c==']') {
                //解码
                StringBuilder sb = new StringBuilder();
                int n = stk2.removeLast();
                for (int i = 0; i < n; i++) {
                    sb.append(current);
                }
                current=sb.toString();
            }else{
                //直接加入
                current+=c;
            }
        }
        return current;
    }
    List<String> res=new ArrayList<>();
    Queue<Integer> str=new LinkedList<>();
    public List<String> restoreIpAddresses(String s) {
        dfs(s,0,0);
        return res;
    }
    public void dfs(String s,int index,int len){
        if(index==4&&s.length()==len){
            //拼接ip地址进行返回
            String ip="";
            for(int i=0;i<str.size();i++){
                ip=str.poll()+".";
            }
            ip=ip.substring(0,ip.length()-1);
            res.add(ip);
            return;
        }
        int num=0;
        for(int j=index;j<s.length();j++){
            if(j==4){break;}
            num=num*10+s.charAt(j)-'0';
            if(num>255){break;}
            str.add(num);
            dfs(s,j+1,len+1);
            str.poll();
            if(num==0){break;}
        }
    }
    public static void method1(){
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                synchronized (lock){
                    while (state%3!=0){
                        //不是等待
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    //是就直接打印
                    System.out.println("A");
                    state++;
                    //并且唤醒其他线程
                    lock.notifyAll();
                }
            }
        }, "T1");
        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                synchronized (lock){
                    while (state%3!=1){
                        //不是等待
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    //是就直接打印
                    System.out.println("B");
                    state++;
                    //并且唤醒其他线程
                    lock.notifyAll();
                }
            }
        }, "T2");
        Thread thread3 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                synchronized (lock){
                    while (state%3!=2){
                        //不是等待
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    //是就直接打印
                    System.out.println("C");
                    state++;
                    //并且唤醒其他线程
                    lock.notifyAll();
                }
            }
        }, "T3");
        thread1.start();
        thread2.start();
        thread3.start();
    }
    public static void method2(){
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                //判断是否到了自己
                lock.lock();
                try {
                    while (state%3!=0){
                        A.await();
                    }
                    System.out.println("A");
                    state++;
                    B.signal();
                }catch (Exception e){

                }finally {
                    lock.unlock();
                }

            }
        }, "T1");
        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                //判断是否到了自己
                lock.lock();
                try {
                    while (state%3!=1){
                        B.await();
                    }
                    System.out.println("B");
                    state++;
                    C.signal();
                }catch (Exception e){

                }finally {
                    lock.unlock();
                }

            }
        }, "T2");
        Thread thread3 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                //判断是否到了自己
                lock.lock();
                try {
                    while (state%3!=2){
                        C.await();
                    }
                    System.out.println("C");
                    state++;
                    A.signal();
                }catch (Exception e){

                }finally {
                    lock.unlock();
                }

            }
        }, "T3");
        thread1.start();
        thread2.start();
        thread3.start();
    }
    public static void method3(){

    }
    public int jump(int[] nums) {
        int count=0;
        //可以走到的最大长度
        int max=0,end=0;
        for (int i = 0; i < nums.length-1; i++) {
            //可以跳跃nums[i]区间
            max=Math.max(max,i+nums[i]);
            if (i==end){
                end=max;
                count++;
            }
        }
        return count;
    }
    public static boolean canJump(int[] nums) {
        //可以跳到的最大的位置
        int max=0;
        String a;
        for (int i = 0; i < nums.length; i++) {
            if(i>max){
                return false;
            }
            max=Math.max(max,nums[i]+i);
        }
        return true;
    }


}
